package com.paradigmcreatives.game.word;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;

import android.util.Log;

import com.paradigmcreatives.game.DoodleGameActivity;

public class WordGenerator {

	DoodleGameActivity doodleGameActivity;
	
	private static final int numberOFWordsToGenerate = 5;

	private static final String TAG = "WordGenerator";
	
	public WordGenerator(DoodleGameActivity doodleGameActivity) {
		this.doodleGameActivity = doodleGameActivity;
	}
	
	public ArrayList<String> getSuggestionWords(){
		ArrayList<String> generatedWords = new ArrayList<String>();
		ArrayList<String> allWords = getAllWords(0);
		int totalWordsCount = allWords.size();
		Log.i(TAG, "Total Words is " + totalWordsCount);
		
		for(int i=0;i<numberOFWordsToGenerate;i++){
			Random random = new Random();
			int currentNumber = random.nextInt(totalWordsCount);
			if(generatedWords.contains(allWords.get(currentNumber))){
				i--;
				continue;
			}else{
				generatedWords.add(allWords.get(currentNumber));
				Log.i(TAG, "Generated word is :: " + generatedWords.get(i));
			}
		}
		
		return generatedWords;
	}
	
	
	private ArrayList<String> getAllWords(int level)
	{
		ArrayList<String> allWords = new ArrayList<String>();
		
		BufferedReader br = null;
	    try {
	        br = new BufferedReader(new InputStreamReader(doodleGameActivity.getAssets().open("words.txt"))); //throwing a FileNotFoundException?
	        String word;
	        while((word=br.readLine()) != null)
	        	allWords.add(word); //break txt file into different words, add to wordList
	    }
	        catch(IOException e) {
	            e.printStackTrace();
	        }
	        finally {
	            try {
	                br.close(); //stop reading
	            }
	            catch(IOException ex) {
	                ex.printStackTrace();
	            }
	        }
		
		return allWords;
	}
	
	
	
}
